#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>


#define SERVER_TCP_PORT 18376
#define SIZE 512

int sigflag;

int err_stop (char* msg)
{
	printf("%s \n",msg);
	exit(-1);
}


int main(int argc, char*argv[ ]) 
{
	
	int sdw, sd2,childpid;

 	int sockfd, clilen,n;

 	struct sockaddr_in serv_addr, cli_addr;  /*we assume this responder is a server and the intiator is a client*/
 	/* message size*/
 	char msg[SIZE];
	char*aurevoir = (char*) malloc (sizeof(char)*20);
	time_t rawtime;

  	// Open a UDP socket (an Internet streams socket) 
	if ( (sockfd = socket (PF_INET, SOCK_STREAM, 0)) < 0){
     	 	err_stop ("server: can't open datagram socket");  //we assume this performs perror() and exit(-1)
		exit(-1);
	}
	
	//Bind our local address so that the client can send to  us

       bzero((char *) &serv_addr, sizeof(serv_addr));
       serv_addr.sin_family      = AF_INET;
       serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
       serv_addr.sin_port        = htons(SERVER_TCP_PORT);


       if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){
		err_stop ("server: can't bind local address");
		exit(-1);	
       }

      /*set up queue for connection request */

	
	//print server port and IP address
	//inet_ntoa convert the server address into a string 
	char* st = (char*)inet_ntoa(serv_addr.sin_addr);
	printf("SERVER ADDRESS : %s \n", st);
	printf("SERVER PORT : %d \n", SERVER_TCP_PORT);
	
	
	sprintf(aurevoir ,"%s ","Au revoir"); 


	//if(listen(sd2,5)<0){
         // perror("listen error in telnetd");
       // }

	
	for(;;){
		sigflag = 0;
	      if(listen(sockfd,5)<0){
         	err_stop("listen error");
         	//exit(-1);
	      }

          	/* Wait for a connection from a client processes.
           	 */
		clilen = sizeof(cli_addr); 
		if ((sd2 = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen)) > 0){
			char *clienthost;
			int cli_port;
			
			clienthost = (char*)inet_ntoa(cli_addr.sin_addr);
        		cli_port = ntohs(cli_addr.sin_port);
        		printf("[Info] : Requete en provenance de l'hoste [%s] dont le port est [%d] \n",clienthost, cli_port);
			
			if((n = recv (sd2, msg,SIZE,0)) > 0){
				
				
				msg[n] = '\0';	

				//printf("%s \n",msg);
				//time_t rawtime;
				
				int compteurs = 0;
				//int msglength = strlen(msg);
				
				for(compteurs = 0;compteurs<60;compteurs++){
					char response[SIZE];
					struct tm * ptm;
					time (&rawtime);
					
					ptm = localtime (&rawtime);							
					
					sprintf(response,"Il est %d : %d :%d ! \n",ptm->tm_hour,ptm->tm_min,ptm->tm_sec); 
					int t;
					
					if( t = (send (sd2, response,strlen(response),0)) < 0){
						err_stop ("server: send error");
						close(sd2);
					}
					//printf("%s", response); 
					sleep(1);
				//	compteurs++;
						
				}
				
				if(compteurs = (send (sd2,aurevoir,strlen(aurevoir),0)) < 0){
						err_stop ("server: send error");
						close(sd2);
				}					
			printf("[Info] : Fin de service du client en provenance de l'hoste [%s] dont le port est [%d] \n",clienthost, cli_port);				
			close(sd2);        	
			}
			else{
				err_stop ("server: an error occur when receiving the message");
				close(sd2);
			} 
			
          	}

	

	}	
	exit(0);
	
}




